# coding: utf-8
#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can rig three span joint leg
#Note: haven't ikfk super swtich.
#FN: rigThreeSpanLeg(type)
#Date: 2012/10/23_v1.0

import maya.cmds as mc
import RosaCommands as rc
import sys
import rosa_StretchTool as rst
import rosa_addShape as ra

#reload(rst)
#-------------------------------------------------------------------------------------------------------------------------------------


def rigThreeSpanLeg(type ):

        #IK RIG SYSTEM RUNNING..........

        # create the base Two sets of joints about iksys
        drvJnt = rc.copyandRenameJoint( type + '_Leg01_Drv' , type+'_leg' , 'drv_jnt')

        ikJnt = rc.copyandRenameJoint( type + '_Leg01_Drv' , type+'_leg' , 'ik_jnt')
        ikbhpJnt = rc.copyandRenameJoint( type + '_Leg01_Drv' , type+'_leg' , 'ikhp_jnt')
        fkctrlJnt = rc.copyandRenameJoint( type + '_Leg01_Drv' , type+'_leg' , 'fk_ctrl' )
        mc.delete(type + '_Leg01_Drv')

        upVecCtrl = mc.rename( type+'_legUp_pole_Drv' , type+'_legUpVec_ctrl')
        mc.select( upVecCtrl )
        rc.makeObjZero('group','zero','On')

        glVecCtrl = mc.rename( type+ '_leg_global_pole_Drv' , type+'_legGlVec_ctrl')
        mc.select( glVecCtrl )
        rc.makeObjZero('group','zero','On')
        mc.setAttr(glVecCtrl+'_zero.visibility' , 0 )

        mc.select(cl=True)


        #create ik system.
        ikGLsys = rc.createikhandle( type+'_leg01_ik_jnt' , type+'_leg04_ik_jnt' , 'ikhandle' , type+'_legGl' )
        ikHpUpsys = rc.createikhandle( type+'_leg01_ikhp_jnt' , type+'_leg03_ikhp_jnt' , 'ikhandle' , type+'_legHpUp' )
        ikHpDnsys = rc.createikhandle( type+'_leg03_ikhp_jnt' , type+'_leg04_ikhp_jnt' , 'ikhandle' , type+'_legHpDn' )

        updnIKNeggrp = mc.createNode('transform' , n = type+'_updnIkNeg_grp')
        mc.parent(updnIKNeggrp , type+'_leg04_ik_jnt')
        rc.zeroAttr( updnIKNeggrp )
        mc.select(updnIKNeggrp)
        uodnIKPosgrp =  rc.makeObjZero('group','zero','On')

        mc.parent(ikHpUpsys[0] , updnIKNeggrp)
        mc.parent(ikHpDnsys[0] , updnIKNeggrp)

        #create ik controls
        mc.select(cl=True)
        ikctrlspace = rc.getSpace( type + '_leg04_drv_jnt' )
        ikctrl = mc.joint( p=(ikctrlspace[0] , ikctrlspace[1] , ikctrlspace[2] ) , n=type+'_legik_ctrl' , radius=0.5 )
        rc.lockhideAttr(ikctrl , 0 ,  'scaleX' , 'scaleY' , 'scaleZ' ,'radius')
        rc.addNewAttr(ikctrl,0,-200,200 , '______','twistUp' , 'twistGL' , 'offset' )
        rc.addNewAttr(ikctrl,0,0,1 , 'stretch')


        mc.orientConstraint( type+'_leg04_drv_jnt' , ikctrl , mo=False)
        mc.delete(cn=True)
        rc.makeObjZero('group','zero','Off')
        mc.makeIdentity( ikctrl , apply=True , t=0 , r=1 , s=0 , n=0 )
        mc.parent(ikGLsys[0] , ikctrl)
        #rc.lockhideAttr(ikctrl , 0 ,  'scaleX' , 'scaleY' , 'scaleZ' ,'radius','visibility')

        #create cmds bridge group
        bridgeGRP = mc.createNode('transform' , name= type+ '_legCmd_bridge')
        rc.lockhideAttr(bridgeGRP , 0 ,  'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility' )
        rc.addNewAttr(bridgeGRP,0,-200,200 , 'twistUp' , 'twistGL' , 'twistAdd' , 'offset' )
        rc.addNewAttr(bridgeGRP,0,0,1 , 'stretch')
        rc.addNewAttr(bridgeGRP,1,0.01,100 , 'globalScale')
        rc.addNewAttr(bridgeGRP,0,0,1 , 'ikfk_switch')
        mc.poleVectorConstraint( upVecCtrl  , ikHpUpsys[0] )

        #connect command for legCmd_bridge
        #connect twistup twistGl twistAdd offset for bridge  (####create a plusMinusAverage node####)
        mc.connectAttr( type+'_legCmd_bridge.twistUp' , ikHpUpsys[0]+'.twist')

        twaddAvNode = mc.createNode('plusMinusAverage' , name = type+'_legGLIK_twad_av')
        mc.setAttr( type+'_legGLIK_twad_av.operation' , 1)
        mc.connectAttr( type+'_legCmd_bridge.twistGL' , type+'_legGLIK_twad_av.input1D[0]')
        mc.connectAttr( type+'_legCmd_bridge.twistAdd' , type+'_legGLIK_twad_av.input1D[1]')
        mc.connectAttr( type+'_legGLIK_twad_av.output1D' , ikGLsys[0]+'.twist' )
        mc.poleVectorConstraint( glVecCtrl , ikGLsys[0] )

        #Judge this property's value

        if type=='Left':
                mc.setAttr(type+'_legCmd_bridge.twistAdd' , -90)

                JudgeRef_Value = rc.getSpace(drvJnt[0])
                JudgeTargetValue = rc.getSpace(ikJnt[0])

                if JudgeTargetValue != JudgeRef_Value:
                        mc.setAttr(type+'_legCmd_bridge.twistAdd' , 90)
                else:
                        pass

                rc.lockhideAttr(type+'_legCmd_bridge' , 1 , 'twistAdd' )

        elif type=='Right':
                mc.setAttr(type+'_legCmd_bridge.twistAdd' , 90)

                JudgeRef_Value = rc.getSpace(drvJnt[0])
                JudgeTargetValue = rc.getSpace(ikJnt[0])

                if JudgeTargetValue != JudgeRef_Value:
                        mc.setAttr(type+'_legCmd_bridge.twistAdd' , -90)
                else:
                        pass

                rc.lockhideAttr(type+'_legCmd_bridge' , 1 , 'twistAdd' )

        #SETUP IK stretch.
        #import myself moudles to help to create stretch joint ,but it is have a bug now: don't have knee lock attribute.
        strNode = rst.Ik_stretch( type+'_leg' , ikGLsys[0] , 3)

        mc.connectAttr( type+'_leg02_ik_jnt.translateX' , type+'_leg02_ikhp_jnt.translateX')
        mc.connectAttr( type+'_leg03_ik_jnt.translateX' , type+'_leg03_ikhp_jnt.translateX')
        mc.connectAttr( type+'_leg04_ik_jnt.translateX' , type+'_leg04_ikhp_jnt.translateX')
        mc.parent( upVecCtrl+'_zero' , type+'_leg02_ik_jnt')
        rc.lockhideAttr(upVecCtrl , 0 ,  'rotateX' , 'rotateY' , 'rotateZ' , 'scaleX' , 'scaleY' , 'scaleZ' ,'radius','visibility')

        #when the stretch after the offset run ,if you don't solve the  question , you can see it is bad.

        ExpandNode = mc.createNode( 'multiplyDivide' , n = type+'_leg_Expand_offset_md')
        mc.connectAttr( type+'_legCmd_bridge.offset' , ExpandNode+'.input1X')
        mc.setAttr( ExpandNode+'.input2X' , 5)
        mc.connectAttr( ExpandNode+'.outputX' , type+'_updnIkNeg_grp_zero.rotateZ')

        rempOffsetValueNode =  mc.createNode( 'remapValue' , n = type+'_leg_rempOffsetValue_rv')
        reverseOffsetNode = mc.createNode( 'multiplyDivide' , n = type+'_leg_reverse_offset_md')
        mc.connectAttr( type+'_updnIkNeg_grp_zero.rotateZ' , reverseOffsetNode+'.input1X')
        mc.setAttr( reverseOffsetNode+'.input2X' , -1)
        mc.connectAttr(  reverseOffsetNode+'.outputX' , rempOffsetValueNode+'.outputMax')
        mc.connectAttr( strNode[0][4]+'.outputX' , rempOffsetValueNode+'.inputValue')
        mc.connectAttr( rempOffsetValueNode+'.outValue' , type+'_updnIkNeg_grp.rotateZ')


        #create ik transform clean group.
        ikglgrp = mc.createNode( 'transform' , n=type+ '_leg_IkRig_grp' )
        ikgrp = mc.createNode( 'transform' , n=type+ '_leg_ikFinal_grp' )
        ikhpgrp = mc.createNode( 'transform' , n=type+'_leg_ikhlpFinal_grp')
        ikctrlgrp = mc.createNode( 'transform' , n = type+'_leg_ikctrl_grp')
        grplist = [ikgrp , ikhpgrp ,  ikctrlgrp]
        mc.parent( grplist , ikglgrp)

        mc.parent( ikJnt[0] , ikgrp)
        mc.parent( ikbhpJnt[0] , ikhpgrp)

        ikctrllist = [  glVecCtrl+'_zero' , ikctrl+'_zero' ,  strNode[0][0] , strNode[1]]
        mc.parent( ikctrllist , ikctrlgrp)
        #bridgeGRP

        #connect IKcontrol with bridge
        mc.connectAttr(ikctrl+'.twistUp' , bridgeGRP+'.twistUp')
        mc.connectAttr(ikctrl+'.twistGL' , bridgeGRP+'.twistGL')
        mc.connectAttr(ikctrl+'.offset' , bridgeGRP+'.offset')
        mc.connectAttr(ikctrl+'.stretch' , bridgeGRP+'.stretch')
        rc.lockhideAttr(ikctrl , 1 ,  '______')





        #FK RIG SYSTEM RUNNING..........

        fkglgrp = mc.createNode( 'transform' , n=type+ '_leg_FkRig_grp' )
        mc.parent(fkctrlJnt[0] , fkglgrp)

        # Uh........ , I really can not have more settings of the fk control........

        #IKFK change SYSTEM RUNNING

        switchCmdGrp = rc.drvCtr_swich(type+'_leg' , ikbhpJnt[0] , fkctrlJnt[0] , type + '_leg01_drv_jnt' , 1)

        rc.setDrv('ikfkswitch' , ikglgrp+'.visibility',0   ,    switchCmdGrp+'.ikfkswitch',0)
        rc.setDrv('ikfkswitch' , ikglgrp+'.visibility',1   ,    switchCmdGrp+'.ikfkswitch',1)

        rc.setDrv('ikfkswitch' , fkglgrp+'.visibility',0   ,    switchCmdGrp+'.ikfkswitch',1)
        rc.setDrv('ikfkswitch' , fkglgrp+'.visibility',1   ,    switchCmdGrp+'.ikfkswitch',0)

        rc.setDrv('ikfkswitch' , ikctrl+'.visibility',1   ,    switchCmdGrp+'.ikfkswitch',1)
        rc.setDrv('ikfkswitch' , ikctrl+'.visibility',0   ,    switchCmdGrp+'.ikfkswitch',0)

        rc.lockhideAttr(ikctrl , 0 ,  'visibility')
        #ikfkchangeCtrl  = mc.joint()


        #clean ikfk setup


        for x in fkctrlJnt:
                rc.lockhideAttr(x , 0, 'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' ,'radius')

        rc.lockhideAttr(switchCmdGrp , 0, 'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'visibility')

        drvglgrp = mc.createNode( 'transform' , n=type+ '_leg_drvRig_grp' )


        mc.connectAttr(bridgeGRP+'.ikfk_switch' , switchCmdGrp+'.ikfkswitch')
        mc.connectAttr(bridgeGRP+'.globalScale' , strNode[0][0]+'.globalScale')
        mc.connectAttr(bridgeGRP+'.stretch' , strNode[0][0]+'.stretch')

        mc.parent(type+'_leg01_drv_jnt' , drvglgrp)
        mc.setAttr(drvglgrp+'.visibility' , 0)

        RigGrp = mc.createNode('transform' , n=type+ '_legRig_grp' )

        mc.connectAttr( RigGrp+'.scaleY' , RigGrp+'.scaleX')
        mc.connectAttr( RigGrp+'.scaleY' , RigGrp+'.scaleZ')
        rc.lockhideAttr(RigGrp, 0 , 'scaleX' , 'scaleZ')
        mc.connectAttr(RigGrp+'.scaleY' , bridgeGRP+'.globalScale')

        mc.parent(drvglgrp , RigGrp)
        mc.parent(fkglgrp , RigGrp)
        mc.parent(ikglgrp , RigGrp)
        mc.parent(bridgeGRP , RigGrp)
        mc.parent(switchCmdGrp , drvglgrp)


        #Add curve shape.


        crvscale = mc.getAttr('Space_Ctr.SCALE')

        #add fk ctrl shapeNode...
        for i in range ( len(fkctrlJnt) ):
                ra.addShape( fkctrlJnt[i], 'D07_circle' , 17 )
                mc.select( fkctrlJnt[i]+'Shape.cv[0:7]' )
                mc.scale(10*crvscale , 10*crvscale , 10*crvscale)
        mc.select( fkctrlJnt[3]+'Shape.cv[0:7]' )
        mc.rotate(0,0,90)
        mc.select(cl=True)

       #add ik ctrl shapeNode...
        ra.addShape( upVecCtrl, 'C00_sphere' , 17 )
        mc.select( upVecCtrl+'Shape.cv[0:48]' )
        mc.scale(5*crvscale , 5*crvscale , 5*crvscale )
        mc.select(cl=True)

        ra.addShape( ikctrl, 'D07_circle' , 17 )
        mc.select( ikctrl+'Shape.cv[0:7]' )
        mc.rotate(0,0,90)
        mc.scale(10*crvscale , 10*crvscale , 10*crvscale )

        settingctrlPos = rc.getSpace(drvJnt[3])
        settingctrl = mc.joint(p=(settingctrlPos[0],settingctrlPos[1],settingctrlPos[2]) , n = type+'_legSetting_ctrl')
        mc.parent(settingctrl , w=True)
        ra.addShape (settingctrl , 'C01_cube' , 17)
        mc.select(settingctrl+'Shape.cv[0:15]' )
        mc.scale(3*crvscale , 3*crvscale , 3*crvscale )
        mc.select(settingctrl)
        rc.makeObjZero('group' , 'zero' , 'On')
        if type=='Left':
                mc.setAttr(settingctrl+'.translateX'  , -12*crvscale)
        elif type =='Right':
                mc.setAttr(settingctrl+'.translateX'  , 12*crvscale)
        mc.parentConstraint( drvJnt[3]  ,settingctrl ,mo=True)
        #mc.Constraint ( drvJnt[3]  ,settingctrl, mo=True)
        mc.parent(settingctrl+'_zero' , RigGrp)
        rc.lockhideAttr(settingctrl , 0 , 'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'visibility','radius')
        rc.lockhideAttr(settingctrl+'_zero' , 0, 'translateX' , 'translateY' , 'translateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'rotateX' , 'rotateY' , 'rotateZ', 'visibility')
        rc.addNewAttr(settingctrl , 0,0,1 , 'ik_fk')
        rc.addNewAttr(settingctrl , 30,-200,200 , 'toelift' )
        rc.addNewAttr(settingctrl , 60,-200,200 ,'toestraight')
        rc.lockhideAttr(fkctrlJnt[1] , 0 , 'rotateX' , 'rotateY' ,'visibility')
        rc.lockhideAttr(fkctrlJnt[2] , 0 , 'rotateX' , 'rotateY' ,'visibility')

        mc.connectAttr(settingctrl+'.ik_fk' , bridgeGRP+'.ikfk_switch')
        mc.select(cl=True)
        #mc.delete('threeSpanLeg_AutoRig_Panel_GRP')


































